#include <stdio.h>
#include <stdlib.h>

typedef struct point
{
    int x, y;
};

void search_path(int map[8][8], point h)
{
    point stack[500];
    int top = -1;
    int flag[500] = {0};
    stack[++top] = h;
    // map[h.x][h.y] = 1;
    while (top >= 0)
    {
        if (top == 64)
        {
            int map_copy[8][8];
            for (int i = 0; i < 64; i++)
            {
                printf("<%d,%d>\n", stack[i].x, stack[i].y);
                map_copy[stack[i].y][stack[i].x] = i + 1;
            }
            for (int m = 0; m < 8; m++)
            {
                for (int n = 0; n < 8; n++)
                {
                    printf("%4d", map_copy[m][n]);
                }
                printf("\n");
            }

            break;
        }
        else
        {
            point p = stack[top];
            point t;
            switch (flag[top])
            {
            case 0:

                flag[top]++;
                t.x = p.x + 1;
                t.y = p.y - 2;
                if (t.x >= 0 && t.x <= 7 && t.y >= 0 && t.y <= 7)
                {
                    if (!map[t.x][t.y])
                    {
                        map[t.x][t.y] = 1;
                        stack[++top] = t;
                    }
                }
                break;
            case 1:

                flag[top]++;
                t.x = p.x + 2;
                t.y = p.y - 1;
                if (t.x >= 0 && t.x <= 7 && t.y >= 0 && t.y <= 7)
                {
                    if (!map[t.x][t.y])
                    {
                        map[t.x][t.y] = 1;
                        stack[++top] = t;
                    }
                }
                break;
            case 2:

                flag[top]++;
                t.x = p.x + 2;
                t.y = p.y + 1;
                if (t.x >= 0 && t.x <= 7 && t.y >= 0 && t.y <= 7)
                {
                    if (!map[t.x][t.y])
                    {
                        map[t.x][t.y] = 1;
                        stack[++top] = t;
                    }
                }
                break;
            case 3:

                flag[top]++;
                t.x = p.x + 1;
                t.y = p.y + 2;
                if (t.x >= 0 && t.x <= 7 && t.y >= 0 && t.y <= 7)
                {
                    if (!map[t.x][t.y])
                    {
                        map[t.x][t.y] = 1;
                        stack[++top] = t;
                    }
                }
                break;
            case 4:

                flag[top]++;
                t.x = p.x - 1;
                t.y = p.y + 2;
                if (t.x >= 0 && t.x <= 7 && t.y >= 0 && t.y <= 7)
                {
                    if (!map[t.x][t.y])
                    {
                        map[t.x][t.y] = 1;
                        stack[++top] = t;
                    }
                }
                break;
            case 5:

                flag[top]++;
                t.x = p.x - 2;
                t.y = p.y + 1;
                if (t.x >= 0 && t.x <= 7 && t.y >= 0 && t.y <= 7)
                {
                    if (!map[t.x][t.y])
                    {
                        map[t.x][t.y] = 1;
                        stack[++top] = t;
                    }
                }
                break;
            case 6:

                flag[top]++;
                t.x = p.x - 2;
                t.y = p.y - 1;
                if (t.x >= 0 && t.x <= 7 && t.y >= 0 && t.y <= 7)
                {
                    if (!map[t.x][t.y])
                    {
                        map[t.x][t.y] = 1;
                        stack[++top] = t;
                    }
                }
                break;
            case 7:

                flag[top]++;
                t.x = p.x - 1;
                t.y = p.y - 2;
                if (t.x >= 0 && t.x <= 7 && t.y >= 0 && t.y <= 7)
                {
                    if (!map[t.x][t.y])
                    {
                        map[t.x][t.y] = 1;
                        stack[++top] = t;
                    }
                }
                break;
            case 8:
                flag[top] = 0;
                map[p.x][p.y] = 0;
                top--;
                break;
            default:
                break;
            }
        }
    }
}

int main()
{
    /*******************code*******************/
    int map[8][8] = {0};
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            map[i][j] = 0;
        }
    }

    point h;
    h.x = 3;
    h.y = 2;
    search_path(map, h);
    /******************************************/
    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
/* 
  40  37  42  33   2  35  18  21
  43  58  39  36  19  22   3  16
  38  41  32  57  34  17  20   9
  59  44  49  54  23  10  15   4
  50  31  60  45  56  25   8  11
  61  48  55  24  53  14   5  26
  30  51  46  63  28   7  12   0
  47  62  29  52  13  64  27   6
   */